<?php

namespace App\Admin\Controllers;

use App\Enums\AvailabilityEnum;
use App\Models\KefuModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class KefuController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new KefuModel(), function (Grid $grid) {
//            $grid->column('id')->sortable();
            $grid->model()->orderBy('id','desc');
            $grid->column('name');

            $grid->column('image')->image(config('app.url').'/storage',80);
            $grid->column('id','跳转页面')->display(function ($id){
                return url('show/'.$id);
            })->copyable();
            $grid->column('url');
            $grid->column('show_times');
            $grid->column('click_times');
            $grid->column('status')->switch();
            $grid->column('updated_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name');

            });
            $grid->disableRowSelector();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new KefuModel(), function (Show $show) {
            $show->field('id');
            $show->field('status');
            $show->field('name');
            $show->field('url');
            $show->field('image');
            $show->field('show_times');
            $show->field('click_times');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new KefuModel(), function (Form $form) {
            $form->radio('status')->options(AvailabilityEnum::descriptions())
                ->default(1);
            $form->text('name')->required()->maxLength(15);
            $form->url('url');
            $form->image('image')->accept('png,jpeg,jpg')->disk('public')
                ->uniqueName()->autoUpload()->removable(false)->retainable()
                ->required();;

        });
    }
}
